import { _decorator, Animation, AudioClip, AudioSource, Component, Node, Sprite } from 'cc';
import { BirdHeadDirection, BirdLevelType } from './commons/GameConst';
import { GameData } from './commons/GameData';
const { ccclass, property } = _decorator;

@ccclass('Bird')
export class Bird extends Component {

    @property([Node])
    birds: Node[] = [];

    private _currentShowBirdNode: Node = null;
    private _currentShowBirdAudioSource: AudioSource = null;
    private _currentBirdLevel: BirdLevelType = BirdLevelType.BLT_Yello;

    private _dieMusic:AudioSource = null;

    initWithData(){
        if(this.birds.length == 0){
            return;
        }
       
        for(let i = 0; i < this.birds.length; i++){
            const bird = this.birds[i];
            if(!bird){
                continue;
            }
            if(i == 0){//默认为一开始是第一只
                bird.active = true;
                this._currentShowBirdNode = bird;
                this._currentShowBirdAudioSource = bird.getComponent(AudioSource);

                let ani = bird.getComponent(Animation);
                if(ani){
                    ani.play();
                }
            }else{
                bird.active = false;
                let ani = bird.getComponent(Animation);
                if(ani){
                    ani.stop();
                }
            }
        }
    }

    public setBirdHeadDirect(directorType: BirdHeadDirection){
        if(!this._currentShowBirdNode){
            return;
        }

        if(directorType == BirdHeadDirection.BH_Middle){
            this._currentShowBirdNode.angle = 0;
        }else if(directorType == BirdHeadDirection.BH_Up){
            this._currentShowBirdNode.angle = 20;
        }else if(directorType == BirdHeadDirection.BH_Down){
            this._currentShowBirdNode.angle = -10;
        }        
    }

    public stopPlay(){
        if(!this._currentShowBirdNode){
            return;
        }

        let ani = this._currentShowBirdNode.getComponent(Animation);
        if(ani){
            ani.stop();
        }
    }

    public Play(){
    
        if(!this._currentShowBirdNode){
            return;
        }

        let ani = this._currentShowBirdNode.getComponent(Animation);
        if(ani){
            ani.play();
        }
    }

    public playDieSouce(){

        if(!this._dieMusic){
            return;
        }

        this._dieMusic.play();
    }

    public playWingSource(){
        this._currentShowBirdAudioSource.play();
    }

    start() {
        this.initWithData();

        let  source = this.node.getComponent(AudioSource);
        if(source){
            this._dieMusic = source;
        }
    }

    update(deltaTime: number) {
        if (this.birds.length == 0) {
            return;
        }

        let curBirdLevel = GameData.instance.getBirdLevel();

        if (this._currentBirdLevel != curBirdLevel) {
            this._currentBirdLevel = curBirdLevel;

            for (let i = 0; i < this.birds.length; i++) {
                const bird = this.birds[i];
                if (!bird) {
                    continue;
                }
                if (i == this._currentBirdLevel) {
                    this._currentShowBirdNode = bird;
                    this._currentShowBirdAudioSource = bird.getComponent(AudioSource);
                    bird.active = true;
                    let ani = bird.getComponent(Animation);
                    if (ani) {
                        ani.play();
                    }
                }else{
                    bird.active = false;
                    let ani = bird.getComponent(Animation);
                    if (ani) {
                        ani.stop();
                    }
                }

            }
        }
    }

}


